<?php

/**
 * organisational_entity actions.
 *
 * @package    wildlive
 * @subpackage organisational_entity
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class organisational_entityActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  	$roots = OrganisationalEntityTable::getInstance()->getRoots();
    $treeObject = Doctrine_Core::getTable('OrganisationalEntity')->getTree();
  	foreach($roots as $root) {
  		$col = $treeObject->fetchTree(array('root_id' => $root->getId()));
    	if($this->organisational_entities instanceof Doctrine_Collection) {
    		$this->organisational_entities->merge($col);
    	} else {
    		$this->organisational_entities = $col;
    		#echo get_class($this->organisational_entities);
    	}
  	}
    $this->max_level = OrganisationalEntityTable::getInstance()->getMaxLevel();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->organisational_entity);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new OrganisationalEntityForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new OrganisationalEntityForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array($request->getParameter('id'))), sprintf('Object organisational_entity does not exist (%s).', $request->getParameter('id')));
    $this->form = new OrganisationalEntityForm($organisational_entity);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array($request->getParameter('id'))), sprintf('Object organisational_entity does not exist (%s).', $request->getParameter('id')));
    $this->form = new OrganisationalEntityForm($organisational_entity);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($organisational_entity = Doctrine_Core::getTable('OrganisationalEntity')->find(array($request->getParameter('id'))), sprintf('Object organisational_entity does not exist (%s).', $request->getParameter('id')));
    $organisational_entity->delete();

    $this->redirect('organisational_entity/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $organisational_entity = $form->save();
      $parent_id = $form->getValue('parent_id');
      if($parent_id > 0) {
      	$parent = OrganisationalEntityTable::getInstance()->findOneById($parent_id);
      	if(!$organisational_entity->isNew()) {
      		$organisational_entity->getNode()->detach();
      	}
	    $parent->getNode()->addChild($form->getObject());
	    $parent->save();
      	$form->getObject()->setRootId($parent->getRootId());
      } else {
      	// new root node
      	$tree = OrganisationalEntityTable::getInstance()->getTree();
      	$tree->createRoot($organisational_entity);
      	$organisational_entity->setRootId($organisational_entity->getId());
      }
      $organisational_entity->save();

      $this->redirect('organisational_entity/edit?id='.$organisational_entity->getId());
    }
  }
}
